#!/usr/bin/env python
# -*- coding: utf-8 -*-

# thirdpart modules
from PyQt5.QtCore import (
    QDataStream,
    QEvent,
    QIODevice,
    QModelIndex,
    Qt,
)

from PyQt5.QtGui import QStandardItemModel

from PyQt5.QtWidgets import (
    # QMessageBox,
    QAbstractItemView,
    QGroupBox,
    QHBoxLayout,
    QHeaderView,
    QPushButton,
    QTableView,
    QVBoxLayout,
    QWidget,
)

class DragTableView(QTableView):
    def __init__(self):
        super().__init__()
        self.viewport().installEventFilter(self)
        self.setDragDropMode(QAbstractItemView.DragDrop)
        self.setDragEnabled(True)
        self.setAcceptDrops(True)
        self.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.setDropIndicatorShown(True)

    def eventFilter(self, obj, event):
        if obj == self.viewport():
            if event.type() == QEvent.Drop:
                mime = event.mimeData()
                encodedata = mime.data("application/x-qabstractitemmodeldatalist")
                if encodedata.isEmpty():
                    return False

                stream = QDataStream(encodedata, QIODevice.ReadOnly)
                while not stream.atEnd():
                    row = stream.readInt()
                    col = stream.readInt()
                    roleDataMap = stream.readQVariantMap()
                    print(row, col, roleDataMap)

                event.setDropAction(Qt.MoveAction)
                event.accept()

        return QWidget.eventFilter(self, obj, event)

class Window(QWidget):
    def __init__(self, title=None):
        super().__init__()
        self.setWindowFlags(Qt.FramelessWindowHint)

        # --------------------------------------------------
        self.groupBoxView = QGroupBox(title)
        self.view = DragTableView()
        self.view.setAlternatingRowColors(True)
        self.view.setSortingEnabled(True)
        self.view.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)

        header = self.view.horizontalHeader()
        header.setSectionsClickable(True)
        header.setSectionsMovable(True)
        header.setStretchLastSection(True)
        header.setSectionResizeMode(QHeaderView.Interactive)
        header = self.view.verticalHeader()
        header.setSectionResizeMode(QHeaderView.ResizeToContents)

        viewLayout = QVBoxLayout()
        viewLayout.addWidget(self.view)
        self.groupBoxView.setLayout(viewLayout)

        # --------------------------------------------------
        btnLayout = QHBoxLayout()
        btnLayout.addStretch(1)
        self.pushButton1 = QPushButton('One')
        self.pushButton1.clicked.connect(self.ShowMessageBox)
        self.pushButton2 = QPushButton('Two')
        btnLayout.addWidget(self.pushButton1)
        btnLayout.addWidget(self.pushButton2)

        self.groupBoxBtn = QGroupBox('Buttons')
        self.groupBoxBtn.setLayout(btnLayout)

        # --------------------------------------------------
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.groupBoxView)
        mainLayout.addWidget(self.groupBoxBtn)
        self.setLayout(mainLayout)

        # --------------------------------------------------
        self.setWindowTitle(title)
        self.setDataModel()
        self.view.resizeColumnsToContents()
        self.view.resizeRowsToContents()
        self.view.updateGeometry()

    def ShowMessageBox(self):
        # print('message box')
        # QMessageBox.information(self, "Message", "this is a message")
        # print(self.model)
        # self.model.moveRow(QModelIndex(), 1, QModelIndex(), 3)
        # self.model.removeRow(0)
        print(self.model.moveRow(QModelIndex(), 0, QModelIndex(), 3))

    def setDataModel(self):
        self.createMailModel()
        self.view.setModel(self.model)

    def addRow(self, rowNum, table_name, column_name, data_type, nullable, column_id):
        model = self.model
        model.setData(model.index(rowNum, 0), table_name)
        model.setData(model.index(rowNum, 1), column_name)
        model.setData(model.index(rowNum, 2), data_type)
        model.setData(model.index(rowNum, 3), nullable)
        model.setData(model.index(rowNum, 4), column_id)

    def createMailModel(self):
        self.model = QStandardItemModel(7, 5, self)
        self.model.setHeaderData(0, Qt.Horizontal, 'TABLE_NAME')
        self.model.setHeaderData(1, Qt.Horizontal, 'COLUMN_NAME')
        self.model.setHeaderData(2, Qt.Horizontal, 'DATA_TYPE')
        self.model.setHeaderData(3, Qt.Horizontal, 'NULLABLE')
        self.model.setHeaderData(4, Qt.Horizontal, 'COLUMN_ID')

        self.addRow(0, 'DP_RULE0', 'DP_ID',             'NUMBER',   'N', 1)
        self.addRow(1, 'DP_RULE1', 'ACCT_ITEM_TYPE_ID', 'NUMBER',   'Y', 2)
        self.addRow(2, 'DP_RULE2', 'RULE_SCRIPT',       'CLOB',     'N', 3)
        self.addRow(3, 'DP_RULE3', 'RULE_COMMENTS',     'VARCHAR2', 'Y', 4)
        self.addRow(4, 'DP_RULE4', 'SP_ID',             'NUMBER',   'Y', 5)
        self.addRow(5, 'DP_RULE5', 'SCRIPT_PAGE',       'BLOB',     'Y', 6)
        self.addRow(6, 'DP_RULE6', 'SCRIPT_TEMPLET_ID', 'NUMBER',   'Y', 7)

if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
